//
// Created by 25429 on 2021/2/3.
//

#include "sirius_engine.h"

void sirius_engine::go() {
    QFile config_file(SIRIUS_CONFIG_PATH);
    if (!config_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        LOG4CPLUS_ERROR(MY_LOGGER, SIRIUS_CONFIG_PATH << " not exist");
        exit(EXIT_FAILURE);
    }

    auto config_str = QTextStream(&config_file).readAll();
    LOG4CPLUS_INFO(MY_LOGGER, "config: " << config_str.toStdString());

    const auto error = new QJsonParseError;
    auto doc = QJsonDocument::fromJson(config_str.toLocal8Bit(), error);
    if (error->error != QJsonParseError::NoError) {
        LOG4CPLUS_ERROR(MY_LOGGER, "config parse error: "
                << error->errorString().toStdString());
        exit(EXIT_FAILURE);
    }

    auto bind_cpu_id = doc[BIND_CUP_ID].toInt();
    common_function::set_bind_cpu_id(bind_cpu_id);

    if (doc.object().contains(SHEN_ZHEN)) {
        engine_config cfg;
        cfg.m_exchange = SHEN_ZHEN;
        cfg.m_params = doc[SHEN_ZHEN].toObject().toVariantMap();

        auto ptr = new md_engine(SHEN_ZHEN, this);
        ptr->m_config = cfg;
        ptr->go();
    }
    if (doc.object().contains(SHANG_HAI)) {
        engine_config cfg;
        cfg.m_exchange = SHANG_HAI;
        cfg.m_params = doc[SHANG_HAI].toObject().toVariantMap();

        auto ptr = new md_engine(SHANG_HAI, this);
        ptr->m_config = cfg;
        ptr->go();
    }
}
